package com.fdm.model.accounts;

import java.util.Map;
import com.fdm.model.exceptions.BankManagerException;
import com.fdm.model.exceptions.InsufficientFundsException;
import com.fdm.model.exceptions.LessThanZeroException;

public class OverDraftLimitDecorator extends AccountDecorator{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4709800235361202835L;
	protected double overDraftLimit;
	
	public OverDraftLimitDecorator(_Account decoratedAccount) {
		super(decoratedAccount);
	}

	public void setOverDraftLimit(Double overdraft) throws LessThanZeroException {
		if (overdraft!=null){
			if (overdraft>=0)
			{
				this.overDraftLimit = overdraft;
			}
			else throw new LessThanZeroException();
		}
	}
	
	@Override
	public void withdraw(Double amount) 
			throws InsufficientFundsException, LessThanZeroException{
		if (amount !=null){
			if (amount >=0)
			{
				if (this.getBalance() - amount + this.getOverDraftLimit()>=0)
				{
					this.setBalance(this.getBalance() - amount);
				}
				else
				{
					throw new InsufficientFundsException();
				}
			}
			else 
			{
				throw new LessThanZeroException();
			}
		}
	}
	
	@Override
	public double getOverDraftLimit() {
		return this.overDraftLimit;
	}
	
	@Override
	public void setAttributes(Map<String, Object> parameters) throws BankManagerException {
		this.setOverDraftLimit( (Double) parameters.get("overDraftLimit"));
	}
	
}
